//
//  XMPicwordPublishViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/7/2.
//

#import "XMPicwordPublishViewController.h"
#import "XMPicwordPublishView.h"
#import "XMNetwork+find.h"
#import "XMImgModel.h"

#import "XMImgSelect.h"
#import "XMPositionSelectViewController.h"

@interface XMPicwordPublishViewController ()<XMPicwordPublishViewDelegate>

@property (nonatomic, strong) XMPicwordPublishView *publishView;

@property (nonatomic, strong) NSMutableDictionary *parameters;

@end

@implementation XMPicwordPublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.publishView];
}

- (void)selectImgFromLibrary {
    [[XMImgSelect sharedInstance] selectImgFromLibraryOnVC:self];
    XMWeakSelf;
    [XMImgSelect sharedInstance].imgSelectBlock = ^(NSArray * _Nonnull imgArr) {
        [weakSelf uploadWithImgArr:imgArr];
    };
}

- (void)selectImgFromCamera {
    [[XMImgSelect sharedInstance] selectImgFromCameraOnVC:self];
    XMWeakSelf;
    [XMImgSelect sharedInstance].imgSelectBlock = ^(NSArray * _Nonnull imgArr) {
        [weakSelf uploadWithImgArr:imgArr];
    };
}

- (void)uploadWithImgArr:(NSArray *)imgArr {
    XMWeakSelf;
    [_Network imgUploadWithImg:imgArr
                     directory:@"picwordPublish"
                       success:^(id  _Nonnull responseObject) {
        NSArray *imgModelArr = [NSArray yy_modelArrayWithClass:[XMImgModel class] json:[responseObject safeArrayForKey:@"data"]];
        [weakSelf.publishView setSelectImgWithImgArr:imgModelArr];
    } failure:^(NSError * _Nonnull error) {
        
    }];;
}

- (void)picwordPublish {
    XMWeakSelf;
    [_Network picwordPublishWithParameters:self.parameters
                                   success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"发布成功"];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
- (void)upLoadImg {
    XMWeakSelf;
    UIAlertController *alertVC = [UIAlertController initwithTitle:@"选择图片" message:@"" titles:@[@"相机", @"相册"] cancelTitle:@"取消" itemSelect:^(NSInteger selectIndex) {
        switch (selectIndex) {
            case 0: {
                [weakSelf selectImgFromCamera];
            }
                break;
            case 1: {
                [weakSelf selectImgFromLibrary];
            }
                break;
            default:
                break;
        }
    }];
    [self presentViewController:alertVC animated:YES completion:nil];
}

//位置选择
- (void)positionSelectAction {
    XMPositionSelectViewController *positionSelVC = [[XMPositionSelectViewController alloc] init];
    [self.navigationController pushViewController:positionSelVC animated:YES];
    XMWeakSelf;
    positionSelVC.positionSelectBlock = ^(CLPlacemark * _Nonnull placemark,
                                          NSString * _Nonnull name) {
        [weakSelf.parameters safe_setObject:[NSString stringWithFormat:@"%lf,%lf", placemark.location.coordinate.longitude, placemark.location.coordinate.latitude] forKey:@"addrPoint"];
        [weakSelf.parameters safe_setObject:placemark.thoroughfare forKey:@"addrDetail"];
        [weakSelf.parameters safe_setObject:placemark.locality forKey:@"addrCity"];
        if (placemark.administrativeArea && placemark.administrativeArea.length > 0) {
            [weakSelf.parameters safe_setObject:placemark.administrativeArea forKey:@"addrProvince"];
        }
        [weakSelf.publishView setPositionSelectInfo:name];
    };
}

- (void)publishWithParameters:(NSMutableDictionary *)parameters {
    [self.parameters addEntriesFromDictionary:parameters];
//    [self.parameters setDictionary:parameters];
    [self picwordPublish];
}

- (XMPicwordPublishView *)publishView {
    if (!_publishView) {
        _publishView = [[XMPicwordPublishView alloc] initWithFrame:self.view.bounds];
        _publishView.delegate = self;
    }
    return _publishView;
}

- (NSMutableDictionary *)parameters {
    if (!_parameters) {
        _parameters = [NSMutableDictionary dictionary];
    }
    return _parameters;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
